<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit" :showHistoryBtn="true">
		<om-autoform ref="autoform" :checkedit="checkedit" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
		<om-white-space bgColor="#eee"></om-white-space>
	</om-addpage>
</template>

<script>
import { getUrl,confirmModal, ptFloat, minusF, errorModal } from '@/static/js/common.js';
import {buildFormData, formValidation,getEditorTitle, hasEditor,tranferToEditors, changeEditor, configSelectData, hasValue,formShowLoading ,getFormFields,formModelLoad,getWorkCacheKey} from '@/static/js/formUtil.js';
import urlUtil from '@/static/js/urlUtil.js';
import { mapState } from 'pinia';
import {useLoginUserStore} from '@/stores/login-user.js';
export default {
	provide() {
		return {
			formPage: this
		};
	},
	data() {
		return buildFormData({
			formCode: "",
		})
	},
	computed: {
		...mapState(useLoginUserStore, ['userName', 'userId'])
	},
	methods: {
		onSelectZpos(item){
			let _that = this
			_that.om.request(
				'tposition/verifyHasSubordinatePos',
				{
					zposId:item.id
				}
			).then(res=>{
				if(res.value){
					errorModal(_that,'提示信息',res.value,()=>{
						_that.$refs.autoform.clear("topinfo","zposId")
						let dataList = []
						_that.subEditorObj["titleInfo"].workList = dataList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"objStr",JSON.stringify(dataList))
						_that.$set(_that.formdata,"objStr",JSON.stringify(dataList))
						delete res[1].value.tpeopleAddApply_datas.dataList
					})
				}
			})
		},
		formSubmit() {
			let _that  = this
			_that.om.formSave('tpeopleAddApply/addTpeopleAddApply',_that.formdata,_that)
		},
		getWorkParam(){
			return {
				zposId:this.formdata.zposId,
				busCode:this.busCode
			}
		},
		initLoad(options) {
			let _that = this;
			var fileId = options.fileId
			var ajaxArr = [
				urlUtil.zposUrl(options.code), 
			];
			if(fileId){
				ajaxArr.push({
					url:"tpeopleAddApply/getInfo",
					param:{
						id:fileId
					}
				})
			}
			formModelLoad(ajaxArr, function(res, mainData) {
				changeEditor('topinfo', 'zposId', 'data', configSelectData(res[0], 'id', 'posname'),mainData);
				if(_that.subEditorObj["titleInfo"]){
						let att = {
							getWorkParam:'getWorkParam'
						}
						let configData = _that.subConfigData['titleInfo']
						if(hasEditor("titleInfo","posId",configData)){
							att.titleTemplate="<%= posName %>"
						}
						if(hasEditor("titleInfo","personNum",configData)){
							let title = getEditorTitle("titleInfo","personNum",configData)
							att.subtitle1Template=title +":<%= personNum %>"
						}
					changeEditor("titleInfo", "objStr", att, mainData)
				}
				if(fileId){
					let fieldArr = getFormFields(mainData)
					fieldArr.push("id")
					_that.formCode = res[1].value.tpeopleAddApply.code;
					uni.setStorageSync('formCodeNum', _that.formCode.toString())
					for(let index in res[1].value.tpeopleAddApply){
						if(fieldArr.includes(index)){
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value.tpeopleAddApply[index])
							_that.$set(_that.formdata,index,res[1].value.tpeopleAddApply[index])
						}
					}
					let dataList = _that._.clone(res[1].value.tpeopleAddApply_datas)
					_that.subEditorObj["titleInfo"].workList = dataList
					_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"objStr",JSON.stringify(dataList))
					_that.$set(_that.formdata,"objStr",JSON.stringify(dataList))
					delete res[1].value.tpeopleAddApply_datas.dataList
				}else{
					_that.formCode = ""
					uni.setStorageSync('formCodeNum', "")
				}
				
				_that.$nextTick(function(){
					if(!_that.$refs.autoform.zposId){
						_that.$refs.autoform.initSelectVal("topinfo","zposId")
					}else{
						_that.$refs.autoform.initSelectIndex("topinfo","zposId")
					}
				})
			}, options)
		},
	},
	onLoad(options) {
		let _that = this;
		_that.initLoad(options);
	},
	onShow() {
		let _that = this;
		if (_that.$refs.autoform) {
			for (var fieldId in _that.subEditorObj) {
				var editor = _that.subEditorObj[fieldId];
				uni.getStorage({
					key: getWorkCacheKey(fieldId),
					success: function(res) {
						editor.workList = res.data;
						uni.removeStorage({
							key: getWorkCacheKey(fieldId)
						});
					}
				});
			}
		}
	}
};
</script>
<style></style>
